package com.whfp.coky.ui.main.frag

import android.content.Context
import android.graphics.Typeface
import android.view.KeyEvent
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import com.hjq.permissions.Permission
import com.whfp.coky.aop.Permissions
import com.whfp.coky.aop.SingleClick
import com.whfp.coky.databinding.FragNonFixedBinding
import com.whfp.coky.databinding.ItemNonFixedTaskBinding
import com.whfp.coky.event.EventCode
import com.whfp.coky.event.EventMessage
import com.whfp.coky.ui.base.BaseAdapter
import com.whfp.coky.ui.base.BaseRecycleFrag
import com.whfp.coky.ui.main.model.TaskCount02Bean
import com.whfp.coky.ui.main.model.TaskNonFixedBean
import com.whfp.coky.ui.main.vm.TaskNonFixedFragVM
import com.whfp.coky.ui.task.act.FluorMainAct
import com.whfp.coky.utils.LogUtil

/**
 *
 *
 * created by coky on 2022/3/3
 */
class TaskNonFixedFrag: BaseRecycleFrag<TaskNonFixedFragVM, FragNonFixedBinding, TaskNonFixedBean>(){

    companion object{
        fun newInstance(): TaskNonFixedFrag{
            return TaskNonFixedFrag()
        }
    }

    override fun initAdapter(context: Context) = NonFixedAdapter(context)

    override fun initRecycler() = vBinding.rvRecycler

    override fun initRefreshLayout() = vBinding.refreshLayout

    override fun initView() {
        super.initView()
//        监听输入。和隐藏
        vBinding.etSearch.let {
            it.setOnEditorActionListener(object : TextView.OnEditorActionListener{
                override fun onEditorAction(tv: TextView?, action: Int, event: KeyEvent?): Boolean {
                    if (action == EditorInfo.IME_ACTION_SEARCH){
                        hideKeyboard(tv)
                        vModel.changeReq(it.text.toString())
                        return true
                    }
                    return false
                }

            })
        }
    }

    fun initTaskCount(count02Bean: TaskCount02Bean?) {
        count02Bean?.let {
            vBinding.tvWc.text = it.completedNum.toString()
            vBinding.tvDb.text = it.executedNum.toString()
        }
    }


    inner class NonFixedAdapter(mContext:Context): BaseAdapter<ItemNonFixedTaskBinding,TaskNonFixedBean>(mContext){
        override fun convert(v: ItemNonFixedTaskBinding, t: TaskNonFixedBean, position: Int) {
            v.sbTask?.let {
                it.getLeftView().typeface = Typeface.DEFAULT_BOLD
                it.getRightView().typeface = Typeface.DEFAULT_BOLD
                it.setLeftText(t.taskName)
                it.setRightText(t.carryNumber.toString())
            }
        }
    }

    @SingleClick
    @Permissions(Permission.CAMERA,Permission.WRITE_EXTERNAL_STORAGE,Permission.READ_EXTERNAL_STORAGE)
    override fun itemClick(pos: Int) {
//        val b = Bundle().apply {
//            this.putSerializable(Constants.BEAN, mAdapter?.getItem(pos))
//        }
//        mAdapter?.getItem(pos)
//        getAttachActivity().startActivity()

//        test scan code
//        getAttachActivity()?.startActivity(
//            ScanCodeAct::class.java,
//            Bundle().apply {
//                this.putSerializable(Constants.TITLE, "")
//                this.putSerializable(Constants.BEAN, TaskChildBean())
//        })

//        test fluor act

        LogUtil.e("为什么不跳转！！！！！！！！！！！")
        getAttachActivity()?.let { FluorMainAct.start(it) }
    }


    override fun handleEvent(msg: EventMessage) {
        super.handleEvent(msg)
        when(msg.code){
            EventCode.SCAN_CODE -> {
                postDelayed({
                    toast("nonfrag收到结果${msg.msg}")
                            },2000)
            }
            else ->toast("nothing")
        }
    }
}


